"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import unittest
#import random
#import string
#from msnp.service.network.Model import PeerInfo
from msnp.service.network.logic import FingerLogic
from msnp.service.network.model.PeerModel import PeerInfo
from msnp.service.utils import Hash
from msnp.service.network import NetworkController
import random
#from msnp.service.utils import Hash

class Fingertable(unittest.TestCase):
    """class to test the fingertable functionality"""
    
    def testCalculateFinger(self):
        hexMyself = str(hex(1121661249504791295698736456178913600364764674400))
        value = FingerLogic.calculateFinger(hexMyself, 155, False)
        print str(long(value, 16))
    
#    def testNearestIdentity(self):
#        identities = self.__createIdentities()
#        myIdentity = PeerInfo(Hash.hash("mySelf"), "pw")
#        NetworkController.getMySelf().setMyIdentity(myIdentity)
#        m = Hash.getHashRange()
#        #create my fingers
#        for i in range(0, m):
#            myIdentity = NetworkController.getMySelf().getMyIdentity()
#            searchedUsername = FingerLogic.calculateFinger(myIdentity.getUsername(), i, True)
#            respIdentity = self.__findResponsibleIdentityForFingerUpdate(identities, searchedUsername, myIdentity, i)
#            NetworkController.getMySelf().setFinger(respIdentity, i)
#        print "\nFingertable:\n"
#        print NetworkController.getMySelf().fingerToString() 
#        print "\nMyself: " + str(long(myIdentity.getUsername(), 16))
#        
#    def __createIdentities(self):
#        """this method creates the simulated ring"""
#        numIdentity = 5
#        strLength = 8
#        identities = []
#        for i in range(0, numIdentity):
#            name = ''.join(random.choice(string.letters) for i in xrange(strLength))
#            name = Hash.hash(name)
#            identity = PeerInfo(name, 'pw')
#            identities.append(identity)
#        identities.sort(key=lambda iden: iden.getUsername(), reverse=False)
#        print "\nSimulated Ring:\n"
#        for identity in identities:
#            print str(long(identity.getUsername(), 16))
#        return identities
#        
#    def __findResponsibleIdentityForFingerUpdate(self, identities, searchedUsername, myIdentity, i):
#        previousIdentity = None
#        for identity in identities:
#            username = identity.getUsername()
#            if username == searchedUsername:
#                return identity
#            elif username > searchedUsername:
#                return previousIdentity
#            previousIdentity = identity
#        if searchedUsername > previousIdentity.getUsername():
#            return identities.get(0)
#        return None
    
def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(Fingertable))
    return suite

if __name__ == '__main__':
    suiteFew = unittest.TestSuite()
    suiteFew.addTest(Fingertable("testCalculateFinger"))
    unittest.TextTestRunner(verbosity=2).run(suite())
